﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Reflection;
using System.Drawing;

namespace SmartWindow
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private static List<SmartButton> smartButtons;
        private String smartWindowPath = System.Environment.GetEnvironmentVariable("SystemDrive") + "\\SmartWindow";
        private String configFIle = System.Environment.GetEnvironmentVariable("SystemDrive") + "\\SmartWindow\\applications.txt";

        public MainWindow()
        {
            //InitializeComponent();
            MessageBox.Show("hola");
            this.Hide();
            smartButtons = new List<SmartButton>();
            /*ResizeMode = ResizeMode.NoResize;
            WindowStyle = WindowStyle.None;
            Topmost = true;
            ShowInTaskbar = false;
            AllowsTransparency = true;
            Height = System.Windows.SystemParameters.PrimaryScreenHeight / 3;
            Width = System.Windows.SystemParameters.PrimaryScreenWidth / 90;
            Left = 0;
            Top = System.Windows.SystemParameters.PrimaryScreenHeight / 4;
            topButton.Height = Height;
            topButton.Width = Width;
            topButton.Content = "S\nm\na\nr\nt\n\nW\ni\nn\nd\no\nw";
            topButton.MouseEnter += new MouseEventHandler(button1_MouseEnter);*/
            createAppsFile();
            getRid();
            SmartApps app = new SmartApps(smartButtons);
            app.Show();
            this.Close();
        }

        private void createAppsFile()
        {
            if (Directory.Exists(smartWindowPath)) { }
            else
            {
                Directory.CreateDirectory(smartWindowPath);
            }

            if (File.Exists(configFIle)) { }
            else
            {
                TextWriter tw = new StreamWriter(configFIle);
                tw.WriteLine("maintenance");
                tw.WriteLine("administrative tools");
                tw.WriteLine("accessibility");
                tw.WriteLine("system tools");
                tw.WriteLine("tablet pc");
                tw.WriteLine("windows powershell");
                tw.WriteLine("about");
                tw.WriteLine("delete");
                tw.WriteLine("documentation");
                tw.WriteLine("document");
                tw.WriteLine("help");
                tw.WriteLine("home");
                tw.WriteLine("idle");
                tw.WriteLine("manual");
                tw.WriteLine("management");
                tw.WriteLine("notes");
                tw.WriteLine("preferences");
                tw.WriteLine("register");
                tw.WriteLine("registration");
                tw.WriteLine("readme");
                tw.WriteLine("remove");
                tw.WriteLine("settings");
                tw.WriteLine("skinned");
                tw.WriteLine("update");
                tw.WriteLine("uninstall");
                tw.WriteLine("website");
                tw.Close();
            }
        }

        private void getRid()
        {
            string[] link = Directory.GetFiles(System.Environment.GetEnvironmentVariable("ProgramData") + @"\Microsoft\Windows\Start Menu\Programs", "*.lnk", SearchOption.AllDirectories);
            string[] readLines = System.IO.File.ReadAllLines(configFIle);

            for (int n = 0; n < link.Length; n++)
            {
                link[n] = link[n].ToLower();
                Boolean hasIt = false;
                foreach (string line in readLines)
                {
                    if (link[n].Contains(line))
                    {
                        hasIt = true;
                        break;
                    }
                }

                if (!hasIt) 
                {
                    smartButtons.Add(new SmartButton(link[n]));
                }
            }

            
            smartButtons.Add(new SmartButton(Properties.Resources.necesitoAlgo, "Necesito algo", Properties.Resources.soundImage));
            smartButtons.Add(new SmartButton(Properties.Resources.ayudame, "Ayúdame", Properties.Resources.soundImage));
            smartButtons.Add(new SmartButton(Properties.Resources.tengoHambre, "Tengo hambre", Properties.Resources.soundImage));
            smartButtons.Add(new SmartButton(Properties.Resources.hola, "Hola", Properties.Resources.soundImage));
            smartButtons.Add(new SmartButton(Properties.Resources.noEntiendo, "No entiendo", Properties.Resources.closeImage));
            smartButtons.Add(new SmartButton(Properties.Resources.quieroIrAlBano, "Quiero ir al baño", Properties.Resources.soundImage));
            smartButtons.Add(new SmartButton(Properties.Resources.bien, "Bien", Properties.Resources.soundImage));
            smartButtons.Add(new SmartButton(Properties.Resources.meDueleAlgo, "Me duele algo", Properties.Resources.soundImage));

            smartButtons.Add(new SmartButton("Cerrar SmartWindow", 1, Properties.Resources.closeImage));
            smartButtons.Add(new SmartButton("Configuración de Puntero", 2, Properties.Resources.settings));
            smartButtons.Add(new SmartButton("Teclado", 3, Properties.Resources.keyboard));

            int dummyButtons = 3 - (smartButtons.Count % 3);
            if (dummyButtons != 0)
            {
                for (int i = 0; i < dummyButtons; i++)
                {
                    smartButtons.Add(new SmartButton("Cerrar SmartWindow", 1, Properties.Resources.closeImage));
                }
            }
        }

        public static List<SmartButton> getSmartButtons()
        {
            return smartButtons;
        }

        /*public void button1_MouseEnter(object sender, MouseEventArgs e)
        {
            SmartApps app = new SmartApps(smartButtons);
            app.Show();
        }*/
    }
}
